#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>

class TopicPublisher01 : public rclcpp::Node {
public:
  // 构造函数
  TopicPublisher01(std::string name) : Node(name) {
    RCLCPP_INFO(this->get_logger(), "%s节点已经启动.", name.c_str());

    /* 创建发布者 */
    command_publisher_ =
        this->create_publisher<std_msgs::msg::String>("command", 10);
    /* 创建定时器 */
    timer_ = this->create_wall_timer(
        std::chrono::milliseconds(500),
        std::bind(&TopicPublisher01::timer_callback, this));
  }

private:
  void timer_callback() {
    /* 创建消息 */
    auto message = std_msgs::msg::String();
    message.data = "forward";
    /* 打印日志 */
    RCLCPP_INFO(this->get_logger(), "发布的消息: %s", message.data.c_str());
    /* 发布消息 */
    command_publisher_->publish(message);
  }

  rclcpp::TimerBase::SharedPtr timer_;
  // 声明话题发布者
  rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_;
};

int main(int argc, char **argv) {
  rclcpp::init(argc, argv);
  /* 创建节点 */
  auto node = std::make_shared<TopicPublisher01>("topic_publisher_01");
  /* 进入一个事件循环 */
  rclcpp::spin(node);
  /* 关闭节点 */
  rclcpp::shutdown();
  return 0;
}